import math,sys,pygame,random
from zombiehealth import ZombieHealthBar

class Zombie():

    screenWidth = 800
    screenHeight = 600
    def __init__(self, pos):
        self.surface = pygame.image.load("rsc/zombies/Zombie2new.png")
        self.surface = pygame.transform.scale(self.surface,(15,25))
        self.decras = 100
        self.rect = self.surface.get_rect()
        self.maxSpeed = 2
        xDir = random.randint(-1,1)
        yDir = random.randint(-1,1)
        self.speed = [self.maxSpeed*xDir,self.maxSpeed*yDir]    
        self.place(pos)
        self.damage = 1
        self.life = 20
        self.maxLife = 20
        self.radius = 10
        self.detectionRadius = 80
        self.didHit = False
        self.living = True
        self.healthbar = ZombieHealthBar(self)

    

    def __str__(self):
        return ""+str(self.rect.rect.cent) + str(self.speed)+ str(self.living)
  
    def distToPoint(self, pt):
        x1 = self.rect.center[0]
        x2 = pt[0]
        y1 = self.rect.center[1]
        y2 = pt[1]
        return math.sqrt(((x2-x1)**2)+((y2-y1)**2))
        #print "I'm near something ", str(zombie.rect.center)
  
    def chase(self,priest):
        if self.distToPoint(priest.rect.center) < self.detectionRadius:
            pX = priest.rect.center[0]
            pY = priest.rect.center[1]
            zX = self.rect.center[0]
            zY = self.rect.center[1]
           
            if pX > zX:
                self.speed[0] = self.maxSpeed
            elif pX < zX:
                self.speed[0] = -self.maxSpeed
            else:
                self.speed[0] = 0
       
            if pY > zY:
                self.speed[1] = self.maxSpeed
            elif pY < zY:
                self.speed[1] = -self.maxSpeed
            else:
                self.speed[1] = 0

    
    def collide_buildings(self, building):
        pass
  
    def collide_wall(self, screenWidth, screenHeight):
        if (self.rect.left < 0
            or self.rect.right > self.screenWidth):
            self.speed[0] = self.speed[0]*-1
        if (self.rect.top < 0
            or self.rect.bottom >self.screenHeight):
            self.speed[1] = self.speed[1]*-1
    
    def collide_zombie(self, zombie):
        if (self.rect.right > zombie.rect.left and self.rect.left < zombie.rect.right):
            if (self.rect.bottom > zombie.rect.top and self.rect.top < zombie.rect.bottom):
                if (self.distToPoint(zombie.rect.center) < self.radius + zombie.radius):
                    if not self.didHit:
                        self.speed[0] = self.speed[0] * -1
                        self.speed[1] = self.speed[1] * -1
                        self.move(None)
                        self.move(None)
                        self.didHit = True
                    if not zombie.didHit:
                        zombie.speed[0] = zombie.speed[0] * -1
                        zombie.speed[1] = zombie.speed[1] * -1
                        zombie.didHit = True
                    return True
        return False
    
    
    def collide_priest(self, priest):
        if (self.rect.right > zombie.rect.left and self.rect.left < priest.rect.right):
            if (self.rect.bottom > zombie.rect.top and self.rect.top < priest.rect.bottom):
                if (self.distToPoint(zombie.rect.center) < self.radius + priest.radius):
                    if not self.didHit:
                        self.speed[0] = self.speed[0] * -1
                        self.speed[1] = self.speed[1] * -1
                        self.move(None)
                        self.move(None)
                        self.didHit = True
                if not zombie.didHit:
                    zombie.speed[0] = zombie.speed[0] * -1
                    zombie.speed[1] = zombie.speed[1] * -1
                    zombie.didHit = True
                    return True
        return False
    
    def collide_attack(self, attack):
        if (self.rect.right > attack.rect.left and self.rect.left < attack.rect.right):
            if (self.rect.bottom > attack.rect.top and self.rect.top < attack.rect.bottom):
                if (self.distToPoint(attack.rect.center) < self.radius + attack.radius):
                    self.life -= attack.damage
                    self.healthbar.update()
                    print "Hit", self.life
                    if self.life <= 0:
                        self.living = False
        return False
  
    def move(self, player):
        if player != None:
            self.chase(player)
        self.rect = self.rect.move(self.speed)
    
    def place(self, point):
        self.rect.center = point
    
    def collide_building(self, building):
        pass
   
    def collide_fatpriest(self,priest):
        pass

    
  
